| Conditions | 1 |
| Paths | 1 |
| Total Lines | 84 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import * as sinon from 'sinon'; |
||
| 11 | describe('serverReducer', () => { |
||
| 12 | const servers = { |
||
| 13 | abc123: { id: 'abc123' }, |
||
| 14 | def456: { id: 'def456' }, |
||
| 15 | }; |
||
| 16 | const expectedFetchServersResult = { type: FETCH_SERVERS, servers }; |
||
| 17 | const ServersServiceMock = { |
||
| 18 | listServers: sinon.fake.returns(servers), |
||
| 19 | createServer: sinon.fake(), |
||
| 20 | deleteServer: sinon.fake(), |
||
| 21 | createServers: sinon.fake(), |
||
| 22 | }; |
||
| 23 | |||
| 24 | describe('reducer', () => { |
||
| 25 | it('returns servers when action is FETCH_SERVERS', () => |
||
| 26 | expect(reducer({}, { type: FETCH_SERVERS, servers })).toEqual(servers)); |
||
| 27 | |||
| 28 | it('returns default when action is unknown', () => |
||
| 29 | expect(reducer({}, { type: 'unknown' })).toEqual({})); |
||
| 30 | }); |
||
| 31 | |||
| 32 | describe('action creators', () => { |
||
| 33 | beforeEach(() => { |
||
| 34 | ServersServiceMock.listServers.resetHistory(); |
||
| 35 | ServersServiceMock.createServer.resetHistory(); |
||
| 36 | ServersServiceMock.deleteServer.resetHistory(); |
||
| 37 | ServersServiceMock.createServers.resetHistory(); |
||
| 38 | }); |
||
| 39 | |||
| 40 | describe('listServers', () => { |
||
| 41 | it('fetches servers and returns them as part of the action', () => { |
||
| 42 | const result = listServers(ServersServiceMock)(); |
||
| 43 | |||
| 44 | expect(result).toEqual(expectedFetchServersResult); |
||
| 45 | expect(ServersServiceMock.listServers.calledOnce).toEqual(true); |
||
| 46 | expect(ServersServiceMock.createServer.called).toEqual(false); |
||
| 47 | expect(ServersServiceMock.deleteServer.called).toEqual(false); |
||
| 48 | expect(ServersServiceMock.createServers.called).toEqual(false); |
||
| 49 | }); |
||
| 50 | }); |
||
| 51 | |||
| 52 | describe('createServer', () => { |
||
| 53 | it('adds new server and then fetches servers again', () => { |
||
| 54 | const serverToCreate = { id: 'abc123' }; |
||
| 55 | const result = createServer(ServersServiceMock, () => expectedFetchServersResult)(serverToCreate); |
||
| 56 | |||
| 57 | expect(result).toEqual(expectedFetchServersResult); |
||
| 58 | expect(ServersServiceMock.createServer.calledOnce).toEqual(true); |
||
| 59 | expect(ServersServiceMock.createServer.firstCall.calledWith(serverToCreate)).toEqual(true); |
||
| 60 | expect(ServersServiceMock.listServers.called).toEqual(false); |
||
| 61 | expect(ServersServiceMock.deleteServer.called).toEqual(false); |
||
| 62 | expect(ServersServiceMock.createServers.called).toEqual(false); |
||
| 63 | }); |
||
| 64 | }); |
||
| 65 | |||
| 66 | describe('deleteServer', () => { |
||
| 67 | it('deletes a server and then fetches servers again', () => { |
||
| 68 | const serverToDelete = { id: 'abc123' }; |
||
| 69 | const result = deleteServer(ServersServiceMock, () => expectedFetchServersResult)(serverToDelete); |
||
| 70 | |||
| 71 | expect(result).toEqual(expectedFetchServersResult); |
||
| 72 | expect(ServersServiceMock.listServers.called).toEqual(false); |
||
| 73 | expect(ServersServiceMock.createServer.called).toEqual(false); |
||
| 74 | expect(ServersServiceMock.createServers.called).toEqual(false); |
||
| 75 | expect(ServersServiceMock.deleteServer.calledOnce).toEqual(true); |
||
| 76 | expect(ServersServiceMock.deleteServer.firstCall.calledWith(serverToDelete)).toEqual(true); |
||
| 77 | }); |
||
| 78 | }); |
||
| 79 | |||
| 80 | describe('createServer', () => { |
||
| 81 | it('creates multiple servers and then fetches servers again', () => { |
||
| 82 | const serversToCreate = values(servers); |
||
| 83 | const result = createServers(ServersServiceMock, () => expectedFetchServersResult)(serversToCreate); |
||
| 84 | |||
| 85 | expect(result).toEqual(expectedFetchServersResult); |
||
| 86 | expect(ServersServiceMock.listServers.called).toEqual(false); |
||
| 87 | expect(ServersServiceMock.createServer.called).toEqual(false); |
||
| 88 | expect(ServersServiceMock.createServers.calledOnce).toEqual(true); |
||
| 89 | expect(ServersServiceMock.createServers.firstCall.calledWith(serversToCreate)).toEqual(true); |
||
| 90 | expect(ServersServiceMock.deleteServer.called).toEqual(false); |
||
| 91 | }); |
||
| 92 | }); |
||
| 93 | }); |
||
| 94 | }); |
||
| 95 |